package com.sinoglobal.ningxia.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.news.NewsDetailActivity;
import com.sinoglobal.ningxia.beans.NewsBannerVo;
import com.sinoglobal.ningxia.beans.NewsBean;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.FlyUtil;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;
import java.util.List;

public class NewsBannerPagerAdapter extends PagerAdapter {
    private List<NewsBannerVo> data; // 存放广告图片的列表
    FinalBitmap fb;
    Bitmap defaultPic;
    String imgUrl = "http://pic1.nipic.com/2009-01-06/200916154838273_2.jpg";
    Activity activity;

    public NewsBannerPagerAdapter(Activity activity, ArrayList<NewsBannerVo> data) {
        super();
        this.activity = activity;
        this.data = data;
        defaultPic = BitmapFactory.decodeResource(activity.getResources(), R.drawable.default21);
        fb = FinalBitmap.create(activity);
    }

    public void setData(List<NewsBannerVo> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    public void clearData() {
        if (data != null) {
            this.data.clear();
            notifyDataSetChanged();
        }
    }

    // 代表的是当前传进来的对象，是不是要在我当前页面显示的
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // Auto-generated method stub
        return arg0 == arg1;// 如果当前显示的View跟你传进来的是同一个View,说明就是要显示的 view
    }

    @Override
    // 图片的数量
    public int getCount() {
        // Auto-generated method stub
        return data == null ? 0 : data.size();
    }

    /**
     * 移除当前一张图片
     */
    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
        // Auto-generated method stub
        view.removeView(data.get(position).getIv());
    }

    /**
     * 添加一张图片
     */
    @Override
    public Object instantiateItem(ViewGroup view, final int position) {
        ImageView iv;
        if (data.get(position).getIv() == null) {
            iv = new ImageView(activity);
            iv.setScaleType(ScaleType.CENTER_CROP);
            data.get(position).setIv(iv);
        }
        iv = data.get(position).getIv();
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, NewsDetailActivity.class);
                NewsBean newsBean = new NewsBean();
                newsBean.setId(data.get(position).getId());
                intent.putExtra(NewsBean.class.getCanonicalName(), newsBean);
                FlyUtil.intentForward(activity, intent);
            }
        });
        fb.display(iv, ConnectionUtil.localUrl + data.get(position).getImg_url(), defaultPic, defaultPic);
        view.addView(iv);

        return iv;
    }
}